package com.punai.customer.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.enums.BusinessType;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusGxChapter;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.service.ICusGxChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工序分包费用项章节Controller
 * 
 * @author zxw
 * @date 2022-12-09
 */
@RestController
@RequestMapping("/cus/gxChapter")
public class CusGxChapterController extends BaseController
{
    @Autowired
    private ICusGxChapterService dmcGxChapterService;

//    @PreAuthorize("@ss.hasPermi('cus:gxChapter:list')")
    @GetMapping("/treeData")
    public AjaxResult treeData()
    {
        List<TreeBo> list = dmcGxChapterService.treeData(getCompanyId());
        return AjaxResult.success(list);
    }

//    @PreAuthorize("@ss.hasPermi('cus:gxChapter:list')")
    @GetMapping("/getSpecialtyList")
    public AjaxResult getSpecialtyList()
    {
        return AjaxResult.success(dmcGxChapterService.getSpecialtyList(getCompanyId()));
    }

//    @PreAuthorize("@ss.hasPermi('cus:gxChapter:list')")
    @GetMapping("/treeDataByTopId/{topId}")
    public AjaxResult treeDataByTopId(@PathVariable String topId)
    {
        List<TreeBo> list = dmcGxChapterService.treeDataByTopId(topId,getCompanyId());
        return AjaxResult.success(list);
    }

    /**
     * 新增工序分包费用项章节
     */
//    @PreAuthorize("@ss.hasPermi('cus:gxChapter:add')")
    @Log(title = CusConstants.GX_NAME + "章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusGxChapter dmcGxChapter)
    {
        if (StrUtil.isEmpty(dmcGxChapter.getPid())) {
            throw new ServiceException("父级id为空");
        }
        return toAjax(dmcGxChapterService.insertDmcGxChapter(dmcGxChapter));
    }

    /**
     * 修改工序分包费用项章节
     */
//    @PreAuthorize("@ss.hasPermi('cus:gxChapter:edit')")
    @Log(title = CusConstants.GX_NAME + "章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusGxChapter dmcGxChapter)
    {
        if (StrUtil.isEmpty(dmcGxChapter.getId())) {
            throw new ServiceException("id为空");
        }
        if (StrUtil.isEmpty(dmcGxChapter.getName())) {
            throw new ServiceException("名称不能为空");
        }
        return toAjax(dmcGxChapterService.updateDmcGxChapter(dmcGxChapter));
    }

    /**
     * 删除工序分包费用项章节
     */
//    @PreAuthorize("@ss.hasPermi('cus:gxChapter:remove')")
    @Log(title = CusConstants.GX_NAME + "章节", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}/{shape}")
    public AjaxResult remove(@PathVariable String id,@PathVariable("shape") String shape)
    {
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("章节id获取失败!");
        }
        if (!StrUtil.containsAny(shape, "N")) {
            throw new ServiceException("参数错误!");
        }
        return toAjax(dmcGxChapterService.deleteDmcGxChapterById(id));
    }

    @GetMapping(value = "/getStaGxChapter/{specId}/{id}")
    public AjaxResult getStaGxChapter(@PathVariable("specId") String specId,@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcGxChapterService.getStaGxChapterById(id,specId,getCompanyId()));
    }

    @Log(title = CusConstants.GX_NAME + "移动章节", businessType = BusinessType.UPDATE)
    @PutMapping("/moveTree/{type}/{id}/{dropId}")
    public AjaxResult moveTree(@PathVariable String type,@PathVariable String id,@PathVariable String dropId)
    {
        dmcGxChapterService.moveTree(type, id, dropId);
        return toAjax(1);
    }
}
